<?php

namespace Trois\Init\Inits;

use EasySwoole\Component\Singleton;
use EasySwoole\EasySwoole\Core;
use EasySwoole\Redis\Config\RedisClusterConfig;
use EasySwoole\Redis\Config\RedisConfig;
use EasySwoole\RedisPool\RedisPool;
use Trois\Design\Observer\IObserver;
use EasySwoole\EasySwoole\Config;

class RedisPoolInit implements IObserver
{
    use Singleton;

    public function update()
    {

        $redisConfig = Config::getInstance()->getConf('REDIS');

        if (isset($redisConfig['clusters'])) {

            $redisClusterPoolConfig = RedisPool::getInstance()->register(new RedisClusterConfig($redisConfig['clusters'], [
                'auth' => $redisConfig['auth'],
            ]));

            try {
                //配置连接池连接数
                $redisClusterPoolConfig->setMinObjectNum(5);
                $redisClusterPoolConfig->setMaxObjectNum(20);
            } catch (\Exception $e) {
                echo "### 系统启动时注入REDIS异常 Msg:{$e->getMessage()} ###";
            }

        } else {

            $redisPoolConfig = RedisPool::getInstance()->register(new RedisConfig($redisConfig));

            try {
                //配置连接池连接数
                $redisPoolConfig->setMinObjectNum(5);
                $redisPoolConfig->setMaxObjectNum(20);
            } catch (\Exception $e) {
                echo "### 系统启动时注入REDIS异常 Msg:{$e->getMessage()} ###";
            }

        }


    }
}
